package mao.leetcode.others;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class leetcode1114 {
    private ReentrantLock lock = new ReentrantLock();
    Condition c1 =  lock.newCondition();
    Condition c2 =  lock.newCondition();
    Condition c3 =  lock.newCondition();
    int n = 1;
    public void Foo() {

    }

    public void first(Runnable printFirst) throws InterruptedException {
        lock.lock();
        while (n != 1){
            c1.await();
        }
        // printFirst.run() outputs "first". Do not change or remove this line.
        printFirst.run();
        n=2;
        c2.signal();
        lock.unlock();

    }

    public void second(Runnable printSecond) throws InterruptedException {
         lock.lock();
         while (n != 2) c2.await();
        // printSecond.run() outputs "second". Do not change or remove this line.
        printSecond.run();
        n=3;
        c3.signal();
        lock.unlock();

    }

    public void third(Runnable printThird) throws InterruptedException {
          lock.lock();
          while (n != 3 ) c3.await();
        // printThird.run() outputs "third". Do not change or remove this line.
        printThird.run();
        n=1;
        c1.signal();
        lock.unlock();

    }
}
